function isIOSClient() {
    var u = navigator.userAgent;
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    return isiOS;
}
function getAppSize(json){
	return isIOSClient()?json.ipa_size:json.apk_size;
}
function getRealLink(channel_link, link) {
	if(isNull(channel_link) == "") {
		return link;
	}
	return channel_link;
}
function isNull(thedata) {
    return (thedata == "" || thedata == undefined || thedata == null) ? "" : thedata;
}

function download(target,isIOS, ios_link, android_link) {
    if (isIOS) {
        if (isNull(ios_link) == '') {
            alert('ios链接为空');
            return;
        }
        target.href = ios_link;
		downloadRequest(target);
        //打开ios链接
        window.open(ios_link);
    } else {
        if (isNull(android_link) == '') {
            alert('adroid链接为空');
            return;
        }
        target.href = android_link;
		downloadRequest(target);
        //打开安卓链接
        window.open(android_link);
    }
}

//格式化时间
function formatTime(shijianchuo) {
    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo * 1000);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    //	return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    return y + '-' + add0(m) + '-' + add0(d);
}

function add0(m) {
    return m < 10 ? '0' + m : m
}

//格式化为1位小数
function formateFixedOne(s){
	return parseFloat(s).toFixed(1);
}

//设置单位rem
document.getElementsByTagName("html")[0].style.fontSize = document.documentElement.clientWidth / 7.5 + "px";
//设置a可以跳转，不然和mui会有冲突
mui('body').on('tap', 'a', function () {
    document.location.href = this.href;
});

var base_url = 'http://api.staging.shidaiwl.cn/api/v1';

// var channel_code = '34998a46-7aca-46b9-b3aa-17ec7dac5275';


//点赞接口
function send_upvote_to_server(discussion_id) {
    request(base_url + '/game/member/discuss/praise?token=' + localStorage.getItem('token'), {
        "type": "json",
        "content": {
            "discussion_id": discussion_id
        }
    }, "post")
        .done(function (data) {
            console.log('点赞成功');
        })
        .fail(function () {
            console.log('点赞失败');
        });
}

//取消点赞接口
function send_cancelupvote_to_server(discussion_id) {

    request(base_url + '/game/member/discuss/praise', {
        "type": "map",
        "content": {
            "discussion_id": discussion_id,
            "token": localStorage.getItem('token')
        }
    }, "delete")
        .done(function (data) {
            console.log('取消点赞成功');
        })
        .fail(function () {
            console.log('取消点赞失败');
        });
}
//评论接口
function send_reply_to_server(content, gameid, parentid) {
    console.log(gameid);
    console.log(content);
    console.log(parentid);
    console.log(localStorage.getItem('token'));

    request(base_url + '/game/member/discuss?token=' + localStorage.getItem('token'), {
        "type": "json",
        "content": {
            "game_id": gameid,
            "content": content,
            "parent_id": parentid
        }
    }, "post")
        .done(function (data) {
            console.log('评论成功');
        })
        .fail(function () {
            console.log('评论失败');
        });

}
//创建评分接口
function send_score_to_server(gameid, score) {
    request(base_url + '/game/member/score?token=' + localStorage.getItem('token'), {
        "type": "json",
        "content": {
            "game_id": gameid,
            "score": score,
        }
    }, "post")
        .done(function (data) {
            console.log('评分成功');
            mui.toast('评分成功');
            localStorage.setItem('score', data.score);
            mui('.icons').off('tap', 'span');
        })
        .fail(function () {
            console.log('评分失败');
        });
}

//封装请求
function request(url, data, type) {
    if (isNull(data) != "") {
        url = data.type == "map" ? url + changeUrl(data) : url;
        data = data.type == "json" ? data.content : {};
    }

    console.log('-----url-----' + url);
    console.log('-----json----' + JSON.stringify(data));

    return $.ajax({
        type: type || 'get',
        url: url,
        data: JSON.stringify(data) || "",
        dataType: "json",
        contentType: "application/json;utf-8",
        timeout: 6000
    });
}

function appfilter(json, isIOSplatform) {
	var i = json.items.length;
	while(i--) {
		if(isIOSplatform) {
			if(isNull(json.items[i].ios_link) == "") {
				json.items.splice(i, 1);
			}
		} else {
			if(isNull(json.items[i].android_link) == "") {
				json.items.splice(i, 1);
			}
		}
	}
	return json;
}
function changeUrl(data) {
    var tmpVal = '?';
    for (var key in data.content) {
        tmpVal += key + '=' + data.content[key] + "&";
    }
    console.log('-----changeUrl-----' + tmpVal);
    return tmpVal;
}